package com.kuiandjie.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kuiandjie.entity.Employee;
import com.kuiandjie.entity.EmployeeProject;
import com.kuiandjie.entity.Log;
import com.kuiandjie.entity.Project;
import com.kuiandjie.entity.Task;
import com.kuiandjie.service.EmployeeProjectService;
import com.kuiandjie.service.LogService;
import com.kuiandjie.service.ProjectService;
import com.kuiandjie.service.TaskService;

public class ProjectServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String curpid = request.getParameter("pid");
		
		if(curpid == null || "".equals(curpid)){
			response.sendRedirect("main.jspx");
		}else{
			
			
			HttpSession session = request.getSession();
			Employee em = (Employee) session.getAttribute("employee");
			
			EmployeeProjectService eps = new EmployeeProjectService();
			EmployeeProject ep = eps.getEPByEmpIdAndProId(em.getId(),curpid);
			
			if(ep == null){
				response.sendError(403,"没有权限访问此网页");
			}else{
				ProjectService ps = new ProjectService();
				Project p = ps.findProById(curpid);
				
				session.setAttribute("curp", p);
				session.setAttribute("role", ep.getRole());
				TaskService ts = new TaskService();
				List<Task> tasklist = ts.findTaskByEidAndPid(em.getId(),curpid);
				
				List<Log> loglist = new LogService().getAllLogByProId(curpid);
				
				request.setAttribute("tasklist", tasklist);
				request.setAttribute("loglist", loglist);
				request.getRequestDispatcher("/WEB-INF/views/project.jsp").forward(request, response);
			}
		}
	}

}
